<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/11/12
 * Time: 12:00
 */
class serv_rapi_pdd extends L {

    public static $account = 'pdd11437989636';

    /**
     * 描述 ：获取拼多多分类
     * 参数 ：{
     *      catId : 分类ID
     *      level : 等级
     * }
     * 作者 ：Zoro.Zhu
     **/
    public static function getCat($params = null) {
        set_time_limit(0);
        $catId = !empty($params['catId']) ? $params['catId'] : 0;
        $level = !empty($params['level']) ? $params['level'] : 1;
        if($level > 3){                                                         //最多4级目录
            return true;
        }
        $token = model_vsAccount::getAccountToken(array('account' => self::$account));
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $data['parent_cat_id'] = $catId;
        $info = model_api_pdd::getCats($data);
        if(!empty($info['goods_cats_list'])){
            foreach($info['goods_cats_list'] as $row){
                L::sql("INSERT INTO `pdd_cat`(
                  `catId`, `catName`, `parentId`, `level`
                ) VALUES (
                  '{$row['cat_id']}', '{$row['cat_name']}', '{$catId}', '{$level}') 
                ON DUPLICATE KEY UPDATE 
                  `catName` = VALUES(`catName`)");
                self::getCat(['catId' => $row['cat_id'] , 'level' => $level + 1]);
            }
        }
    }

    /**
     * 描述 ：获取类目属性
     * 作者 ：Zoro.Zhu
     **/
    public static function getAttrSpec($account){
        $key = 'pddAttrSpec';
        if (!empty(of_base_com_kv::get($key))) {
            $info = of_base_com_kv::get($key);
        } else {
            $token = model_vsAccount::getAccountToken(array('account' => $account));
            $data['accessToken'] = $token['accessToken'];
            $data['clientId'] = $token['appKey'];
            $data['clientSecret'] = $token['appSecret'];
            $data['catId'] = '18496';
            $info = model_api_pdd::getAttrSpec($data);
            $info = json_encode($info);
            of_base_com_kv::set($key , $info, 86400 * 7);
        }
        $data = json_decode($info, true);
        return $data;
    }

    /**
     * 描述 ：获取产品明细
     * 作者 ：Zoro.Zhu
     **/
    public static function getProductInfo() {
        $productId = $_GET['pro'];
        $account = $_GET['acc'];
        $token = model_vsAccount::getAccountToken(array('account' => $account));
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $data['goodsId'] = $productId;
        $info = model_api_pdd::getGoodsDetail($data);
        p($info);
    }

    /**
     * 描述 : 将商品上下架
     * 作者 : Zoro.Zhu
     */
    public static function updProductStatus($pddId, $account, $isOnsale){
        $token = model_vsAccount::getAccountToken(['account' => $account]);
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $data['isOnsale'] = $isOnsale;                                          //0下架1上架
        $data['goodsId'] = $pddId;
        $res = model_api_pdd::updateSaleStatus($data);
        return $res;
    }

    /**
     * 描述 : 删除下架产品
     * 作者 : Zoro.Zhu
     */
    public static function delProduct($pddId, $account){
        $token = model_vsAccount::getAccountToken(['account' => $account]);
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $data['goodsId'] = [$pddId];
        $res = model_api_pdd::deleteCommit($data);
        return $res;
    }


}
return true;